package com.xileyougame.cmdnxapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.ProgressBar;

import com.google.gson.Gson;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private WebView mWebView;
    private Activity mainActivity = this;
    private ProgressBar mProgressBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mProgressBar = findViewById(R.id.progressBar);
        mWebView = findViewById(R.id.webView);

        WebView.setWebContentsDebuggingEnabled(true);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setMediaPlaybackRequiresUserGesture(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setSupportMultipleWindows(true);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        mWebView.setBackgroundColor(Color.TRANSPARENT);

        //mWebView.loadUrl("https://h5sdk-xly.xileyougame.com/index.php/enter/play/" + platform + "?uid=" + entity.getAccount() + "&token=" + entity.getToken());
        mWebView.loadUrl("https://mercury-dev.xileyougame.com/");

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onReceivedError(WebView var1, int var2, String var3, String var4) {
                mProgressBar.setVisibility(View.GONE);
                //ToastUtil.show("网页加载失败");
            }
        });

        //添加 JS调用java的接口
        mWebView.addJavascriptInterface(new Object() {
//            @JavascriptInterface
//            public void getAndroidInfo() {
//                mWebView.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        String mac = getMac(mainActivity);
//                        String imei = getIEMICode(mainActivity);
//                        String androidId = getAndroidId(mainActivity);
//                        String all =  "'"+mac + " | " + imei + " | " + androidId+"'";
//                        mWebView.loadUrl("javascript:getMac('" + mac + "')");
//                        mWebView.loadUrl("javascript:getImei('" + imei + "')");
//                        mWebView.loadUrl("javascript:getAndroidId('" + androidId + "')");
//                    }
//                });
//            }
            @JavascriptInterface
            public void onAccountRegister(String jsonInfo) {
                Gson gson = new Gson();
                AccountRegisterInfo registerInfo = gson.fromJson(jsonInfo, AccountRegisterInfo.class);

            }
            @JavascriptInterface
            public void onAccountLogin(String jsonInfo) {
                Gson gson = new Gson();
                AccountLoginInfo loginInfo = gson.fromJson(jsonInfo, AccountLoginInfo.class);

            }
            @JavascriptInterface
            public void onAccountPay(String jsonInfo) {
                Gson gson = new Gson();
                AccountPayInfo payInfo = gson.fromJson(jsonInfo, AccountPayInfo.class);

            }
            @JavascriptInterface
            public void logOut(String str) {
                mWebView.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        }, "nativereport");

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);
                    //mWebView.setVisibility(View.VISIBLE);
                    return;
                }
                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.setProgress(newProgress);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();

    }


    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public static String getMac(Context context) {
        String address = null;
        // 把当前机器上的访问网络接口的存入 Enumeration集合中
        Enumeration<NetworkInterface> interfaces = null;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while (interfaces.hasMoreElements()) {
            NetworkInterface netWork = interfaces.nextElement();
            // 如果存在硬件地址并可以使用给定的当前权限访问，则返回该硬件地址（通常是 MAC）。
            byte[] by = null;
            try {
                by = netWork.getHardwareAddress();
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (by == null || by.length == 0) {
                continue;
            }
            StringBuilder builder = new StringBuilder();
            for (byte b : by) {
                builder.append(String.format("%02X:", b));
            }
            if (builder.length() > 0) {
                builder.deleteCharAt(builder.length() - 1);
            }
            String mac = builder.toString();
            Log.d("mac", "interfaceName=" + netWork.getName() + ", mac=" + mac);
            if (netWork.getName().equals("wlan0")) {
                Log.d("mac", " interfaceName =" + netWork.getName() + ", mac=" + mac);
                address = mac;
            }
        }
        return address;
    }

    public static String getIEMICode(Context context) {
        String imei = "";
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            imei = telephonyManager.getDeviceId();
        } catch (SecurityException e) {
            e.printStackTrace();
        }

        return imei;
    }

    public static String getAndroidId(Context context) {
        String ANDROID_ID = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
        return ANDROID_ID;
    }


}
